<?php
/**
 * Base model class for models that have acl support
 *
 * @package Custom_Model
 * @version $Id:$
 */
abstract class Custom_Model_Repositories_AclRepositoryAbstract
extends Custom_Model_Repositories_RepositoryAbstract
implements Custom_Model_AclRepositoryInterface, Zend_Acl_Resource_Interface
{

    /**
     * Default role is 'guest' incase there is no role assigned implicitily
     */
    const Custom_Model_Repositories_DEFAULT_ROLE = 'visitor';

    /**
     * @var Zend_Acl
     */
    protected $_acl;

    /**
     * @var string
     */
    protected $_identity;

    /**
     * Set identity from current request
     *
     * @param array|string|null|Zend_Acl_Role_Interface $identity
     * @return Custom_Model_AclRepositoryAbstract
     * @throws Custom_Model_Exception
     */
    public function setIdentity($identity)
    {
        /*if (is_array($identity)) {
            if (!isset($identity['role'])) {
                $identity['role'] = Custom_Constant::USR_ROLE_VISITOR;
            }
            $identity = new Zend_Acl_Role($identity['role']);
        } else if (is_scalar($identity) && !is_bool($_identity)) {
            $identity = new Zend_Acl_Role($identity);
        } else if (null === $identity) {
            $identity = new Zend_Acl_Role(Custom_Constant::USR_ROLE_VISITOR);
        } else if (!$identity instanceof Zend_Acl_Role_Interface) {
            throw new Custom_Model_Exception('Invalid identity provided!');
        }
        $this->_identity = $identity;

        return $this;*/

    }

    /**
     * Get identity, if no iden, uses 'guest'
     *
     * @return string
     */
    public function getIdentity()
    {
        /*if (null === $this->_identity) {
            $auth = Zend_Auth::getInstance();
            if ($auth->hasIdentity()) {
                $this->setIdentity($auth->getIdentity());
            } else {
                $this->setIdentity(Custom_Constant::USR_ROLE_VISITOR);
            }
        }
        return $this->_identity;*/
    }
	
	/**
     * Check the acl for given action
     *
     * @param string $action
     * @return boolean
     */
    public function checkAcl($action)
    {
        //return $this->getAcl()->isAllowed($this->getIdentity(), $this, $action);
    }

    /**
     * Injector for the acl, the acl can be injected either directly
     * via this method or by passing the 'acl' option to the repository
     * construct.
     *
     * @param Custom_Acl_Interface $acl
     * @return Mkk_Model_RepositoryAbstract
     * @throws Custom_Model_Exception
     */
    public function setAcl(Custom_Acl_Interface $acl)
    {
        /*if (!$acl->has($this->getResourceId())) {
            $acl->add($this)            
            ->allow(Mkk_Constant::USR_ROLE_VISITOR, $this, array('list'))
            ->allow(Mkk_Constant::USR_ROLE_USER, $this, array('list'))
            ->allow(Mkk_Constant::USR_ROLE_OPENID_USER, $this, array('list'))
            ->allow(Mkk_Constant::USR_ROLE_WORDPRESS_USER, $this, array('list'))                        
            ->allow(Mkk_Constant::USR_ROLE_ADMIN_FULLRIGHT, $this);            
        }

        $this->_acl = $acl;

        return $this;*/
    }

    /**
     * Get the acl and automatically instantiate the default acl if one
     * has not been injected.
     *
     * @return Zend_Acl
     */
    public function getAcl()
    {
        /*if (null === $this->_acl) {
            $this->setAcl(new Mkk_Model_Acl_Default());
        }
        return $this->_acl;*/
    }

}